﻿using Management.Errorcode;
using Management.Errorcode.ErrorCode;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Management.API.Filter
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class ExceptionFilter: IExceptionFilter
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<GlobalExceptionFilter> _logger;

        /// <summary>
        /// 注入
        /// </summary>
        /// <param name="logger"></param>
        public ExceptionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            _logger = logger;
        }

        public void OnException(ExceptionContext context)
        {
            _logger.LogError("全局异常过滤器发生异常" + context.Exception.Message);
            context.Result = new JsonResult(ApiResult.Fail(context.Exception.Message, StatusCodeEnum.NotFound));
            context.ExceptionHandled = true;
        }


    }
}
